package com.fh.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.menu.entity.UmsMenu;
import com.fh.menu.mapper.UmsMenuMapper;
import com.fh.menu.service.IUmsMenuService;
import com.fh.result.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户权限表 服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-15
 */
@Service
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements IUmsMenuService {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Override
    public ResultObject saveOrUpdateMenu(UmsMenu umsMenu) {
        if(umsMenu.getId()==null){
            umsMenu.setCreateTime(new Date());
        }
        if(umsMenu.getPid()==null){
            umsMenu.setPid(0L);
        }
        saveOrUpdate(umsMenu);
        return ResultObject.success();
    }

    @Override
    public ResultObject deletemenu(Long menuId) {
        List<Long> idList = getIdList(menuId);
        List<UmsMenu> umsPermissionList = idList.stream().map(x -> {
            UmsMenu umsPermission = new UmsMenu();
            umsPermission.setId(x);
            umsPermission.setStatus(0);
            return umsPermission;
        }).collect(Collectors.toList());
        updateBatchById(umsPermissionList);
        return ResultObject.success();
    }

    public List<Long> getIdList(Long permissionId){
        QueryWrapper<UmsMenu> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.eq("pid",permissionId);
        List<UmsMenu> umsPermissionList = umsMenuMapper.selectList(umsPermissionQueryWrapper);
        List<Long> idList = umsPermissionList.stream().map(x -> x.getId()).collect(Collectors.toList());
        idList.add(permissionId);
        return idList;
    }
    @Override
    public ResultObject deleteBach(List<Long> pidList) {
        List<Long> idList = getPidList(pidList);
        List<UmsMenu> umsPermissionList = idList.stream().map(x -> {
            UmsMenu umsPermission = new UmsMenu();
            umsPermission.setId(x);
            umsPermission.setStatus(0);
            return umsPermission;
        }).collect(Collectors.toList());
        updateBatchById(umsPermissionList);
        return ResultObject.success();
    }



    public List<Long> getPidList(List<Long> pidList){
        QueryWrapper<UmsMenu> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.in("pid",pidList);
        List<UmsMenu> permissionList = umsMenuMapper.selectList(umsPermissionQueryWrapper);
        List<Long> idList = permissionList.stream().map(x -> x.getId()).collect(Collectors.toList());
        pidList.forEach(x ->idList.add(x));
        return idList;
    }


    public ResultObject queryMenusZtreeList() {
        List<UmsMenu> list = list();
        List<Map<String, Object>> menusList = getTree(list, 0l);
        return ResultObject.success(menusList);
    }

    private List<Map<String, Object>> getTree(List<UmsMenu> menuList, Long pid) {
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        menuList.forEach(menu->{
            Map<String,Object>map=null;
            if (pid.equals(menu.getPid())){
                map=new HashMap<>();
                map.put("id",menu.getId());
                map.put("label",menu.getName());
                map.put("children",getTree(menuList,menu.getId()));
            }
            if (map!=null){
                list.add(map);
            }
        });
        return list;
    }

}
